<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="82px">
      <el-form-item label="卡号" prop="cardNo">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="请输入卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="商品名称" prop="goodsId">
        <el-select
          v-model="queryParams.goodsId"
          placeholder="请选择商品名称"
          value-key="id"
          filterable
          clearable
        >
        <el-option
          v-for="card in goodsIdList"
          :key="card.id"
          :label="card.goodsName"
          :value="card.id"
        ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="提货人" prop="consignee">
        <el-input
          v-model="queryParams.consignee"
          placeholder="请输入提货人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提货人电话" prop="tel">
        <el-input
          v-model="queryParams.tel"
          placeholder="请输入提货人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="收货地省份" prop="provinces">
        <el-input
          v-model="queryParams.provinces"
          placeholder="请输入收货地省份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货地城市" prop="cities">
        <el-input
          v-model="queryParams.cities"
          placeholder="请输入收货地城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货地区县" prop="counties">
        <el-input
          v-model="queryParams.counties"
          placeholder="请输入收货地区县"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> 
      <el-form-item label="发货时间" prop="sendTime">
        <el-date-picker clearable
          v-model="queryParams.sendTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择发货时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="快递单号" prop="trackingNumber">
        <el-input
          v-model="queryParams.trackingNumber"
          placeholder="请输入快递单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快递公司" prop="expressCompany">
        <el-input
          v-model="queryParams.expressCompany"
          placeholder="请输入快递公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="提货门店" prop="pickupStore">
        <el-select
          v-model="queryParams.pickupStore"
          placeholder="请选择商品名称"
          value-key="id"
          filterable
          clearable
        >
        <el-option
          v-for="store in storeList"
          :key="store.id"
          :label="store.storeName"
          :value="store.id"
        ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:pickup:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:pickup:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:pickup:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:pickup:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pickupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键" align="center" prop="id" /> -->
      <el-table-column label="卡号" align="center" prop="cardNo" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="提货人" align="center" prop="consignee" />
      <el-table-column label="提货人电话" align="center" prop="tel" />
      <el-table-column label="收货地省份" align="center" prop="provinces" />
      <el-table-column label="收货地城市" align="center" prop="cities" />
      <el-table-column label="收货地区县" align="center" prop="counties" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="发货时间" align="center" prop="sendTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="提货门店" align="center" prop="storeName" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="快递单号" align="center" prop="trackingNumber" />
      <el-table-column label="快递公司" align="center" prop="expressCompany" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:pickup:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:pickup:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改在线提货对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="卡号" prop="cardNo" required>
              <el-input v-model="form.cardNo" placeholder="请输入卡号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="100px" label="商品名称" prop="goodsId" required>
              <el-select
                v-model="form.goodsId"
                placeholder="请选择商品名称"
                value-key="id"
                filterable
                clearable
              >
              <el-option
                v-for="card in goodsIdList"
                :key="card.id"
                :label="card.goodsName"
                :value="card.id"
              ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="提货人" prop="consignee" required>
              <el-input v-model="form.consignee" placeholder="请输入提货人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="100px" label="提货人电话" prop="tel" required>
              <el-input v-model="form.tel" placeholder="请输入提货人电话" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="收货地省份" prop="provinces" required>
              <el-input v-model="form.provinces" placeholder="请输入收货地省份" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="100px" label="收货地城市" prop="cities" required>
              <el-input v-model="form.cities" placeholder="请输入收货地城市" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="收货地区县" prop="counties" required>
              <el-input v-model="form.counties" placeholder="请输入收货地区县" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="100px" label="详细地址" prop="address" required>
              <el-input v-model="form.address" type="textarea" placeholder="请输入详细地址" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="快递单号" prop="trackingNumber">
              <el-input v-model="form.trackingNumber" placeholder="请输入快递单号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="100px" label="快递公司" prop="expressCompany">
              <el-input v-model="form.expressCompany" placeholder="请输入快递公司" />
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="发货时间" prop="sendTime">
              <el-date-picker clearable
                v-model="form.sendTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择发货时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="100px" label="提货门店" prop="pickupStore" required>
              <el-select
                v-model="form.pickupStore"
                placeholder="请选择提货门店"
                value-key="id"
                filterable
                clearable
              >
              <el-option
                v-for="store in storeList"
                :key="store.id"
                :label="store.storeName"
                :value="store.id"
              ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label-width="100px" label="货品数量" prop="goodsNum" required>
              <el-input-number v-model="form.goodsNum" :min="1"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="100px" label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPickup, getPickup, delPickup, addPickup, updatePickup } from "@/api/system/pickup";
import { listGoods } from "@/api/system/goods";
import { listStore } from "@/api/system/store";
export default {
  name: "Pickup",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 在线提货表格数据
      pickupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cardNo: null,
        goodsId: null,
        consignee: null,
        tel: null,
        provinces: null,
        cities: null,
        counties: null,
        address: null,
        sendTime: null,
        trackingNumber: null,
        expressCompany: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cardNo: [
            { required: true, message: '请输入卡号', trigger: 'blur' },
        ],
        goodsId: [
            { required: true, message: '请选择商品', trigger: 'blur' },
        ],
        consignee: [
            { required: true, message: '请输入提货人', trigger: 'blur' },
        ],
        tel: [
            { required: true, message: '请输入提货人电话', trigger: 'blur' },
        ],
        provinces: [
            { required: true, message: '请输入提货人省份', trigger: 'blur' },
        ],
        cities: [
            { required: true, message: '请输入提货人城市', trigger: 'blur' },
        ],
        counties: [
            { required: true, message: '请输入提货人区县', trigger: 'blur' },
        ],
        address: [
            { required: true, message: '请输入提货人详细地址', trigger: 'blur' },
        ],
        pickupStore: [
            { required: true, message: '请选择提货门店', trigger: 'blur' },
        ],
        goodsNum: [
            { required: true, message: '请输入提货数量', trigger: 'blur' },
        ],
      },
      // 商品集合列表
      goodsIdList:[],
      // 门店集合
      storeList:[],
    };
  },
  created() {
    this.getList();
    this.getGoodsList();
    this.getStoreList()
  },
  methods: {
    /** 查询在线提货列表 */
    getList() {
      this.loading = true;
      listPickup(this.queryParams).then(response => {
        this.pickupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询茶叶商品列表 */
    getGoodsList() {
      listGoods().then(response => {
        this.goodsIdList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cardNo: null,
        goodsId: null,
        consignee: null,
        tel: null,
        provinces: null,
        cities: null,
        counties: null,
        address: null,
        sendTime: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        trackingNumber: null,
        expressCompany: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加在线提货";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPickup(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改在线提货";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePickup(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPickup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除在线提货编号为"' + ids + '"的数据项？').then(function() {
        return delPickup(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/pickup/export', {
        ...this.queryParams
      }, `pickup_${new Date().getTime()}.xlsx`)
    },
    // 获取全部门店
    getStoreList(){
      listStore().then(response => {
        this.storeList = response.rows;
      });
    },
  }
};
</script>
